﻿@using BasicTestApp.ValidationModels
@using System.ComponentModel.DataAnnotations
@using Microsoft.AspNetCore.Components.Forms

@if(RendererInfo.IsInteractive) {
    <p id="is-interactive"></p>
}

@if (_invalid)
{
    <p id="is-invalid"></p>
}

<EditForm id="add-validation-form" Model="@order" OnValidSubmit="@HandleValidSubmit" OnInvalidSubmit="@HandleInvalidSubmit">
    <DataAnnotationsValidator />

    <div class="container mt-4">
        <h4>Order Details</h4>
        <div class="mb-3">
            <label for="orderName" class="form-label">Order Name</label>
            <InputText id="orderName" @bind-Value="order.OrderName" class="form-control" />
            <ValidationMessage For="@(() => order.OrderName)" />
        </div>

        <hr />

        <h4>Customer Details</h4>
        <div class="card mb-3">
            <div class="card-body">
                <div class="mb-3">
                    <label for="customerFullName" class="form-label">Full Name</label>
                    <InputText id="customerFullName" @bind-Value="order.CustomerDetails.FullName" class="form-control" />
                    <ValidationMessage For="@(() => order.CustomerDetails.FullName)" />
                </div>
                <div class="mb-3">
                    <label for="customerEmail" class="form-label">Email</label>
                    <InputText id="customerEmail" @bind-Value="order.CustomerDetails.Email" class="form-control" />
                    <ValidationMessage For="@(() => order.CustomerDetails.Email)" />
                </div>

                <h5>Payment Address</h5>
                <div class="card mb-3">
                    <div class="card-body">
                        <div class="row">
                            <div class="mb-3 col-sm-8">
                                <label for="paymentStreet" class="form-label">Street</label>
                                <InputText id="paymentStreet" @bind-Value="order.CustomerDetails.PaymentAddress.Street" class="form-control" />
                                <ValidationMessage For="@(() => order.CustomerDetails.PaymentAddress.Street)" />
                            </div>
                            <div class="mb-3 col-sm">
                                <label for="paymentZipCode" class="form-label">Zip Code</label>
                                <InputText id="paymentZipCode" @bind-Value="order.CustomerDetails.PaymentAddress.ZipCode" class="form-control" />
                                <ValidationMessage For="@(() => order.CustomerDetails.PaymentAddress.ZipCode)" />
                            </div>
                        </div>
                    </div>
                </div>

                <h5>Shipping Address</h5>
                <div class="card mb-3">
                    <div class="card-body">
                        <div class="row">
                            <div class="mb-3 col-sm-8">
                                <label for="shippingStreet" class="form-label">Street</label>
                                <InputText id="shippingStreet" @bind-Value="order.CustomerDetails.ShippingAddress.Street" class="form-control" />
                                <ValidationMessage For="@(() => order.CustomerDetails.ShippingAddress.Street)" />
                            </div>
                            <div class="mb-3 col-sm">
                                <label for="shippingZipCode" class="form-label">Zip Code</label>
                                <InputText id="shippingZipCode" @bind-Value="order.CustomerDetails.ShippingAddress.ZipCode" class="form-control" />
                                <ValidationMessage For="@(() => order.CustomerDetails.ShippingAddress.ZipCode)" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <hr />

        <h4>Order Items</h4>
        @if (order.OrderItems.Any())
        {
            for (int i = 0; i < order.OrderItems.Count; i++)
            {
                var itemIndex = i;
                <div class="card mb-3">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <span>Item @(itemIndex + 1)</span>
                        <button type="button" class="btn btn-sm btn-danger" @onclick="() => RemoveOrderItem(itemIndex)">Remove</button>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="mb-3 col-sm-8">
                                <label for="@($"productName_{itemIndex}")" class="form-label">Product Name</label>
                                <InputText id="@($"productName_{itemIndex}")" @bind-Value="order.OrderItems[itemIndex].ProductName" class="form-control" />
                                <ValidationMessage For="@(() => order.OrderItems[itemIndex].ProductName)" />
                            </div>
                            <div class="mb-3 col-sm">
                                <label for="@($"quantity_{itemIndex}")" class="form-label">Quantity</label>
                                <InputNumber id="@($"quantity_{itemIndex}")" @bind-Value="order.OrderItems[itemIndex].Quantity" class="form-control" />
                                <ValidationMessage For="@(() => order.OrderItems[itemIndex].Quantity)" />
                            </div>
                        </div>
                    </div>
                </div>
            }
        }
        else
        {
            <p>No order items. Add one below.</p>
        }

        <button type="button" class="btn btn-success mb-3" @onclick="AddOrderItem">Add Order Item</button>

        <hr />

        <div class="mb-3">
            <button type="submit" id="submit-form" class="btn btn-primary">Submit Order</button>
        </div>

        <ValidationSummary />
    </div>
</EditForm>

@code {
    private OrderModel order = new OrderModel();
    private bool _invalid;

    private void HandleValidSubmit()
    {
    }

    private void HandleInvalidSubmit()
    {
        _invalid = true;
    }

    private void AddOrderItem()
    {
        order.OrderItems.Add(new OrderItemModel());
    }

    private void RemoveOrderItem(int index)
    {
        if (index >= 0 && index < order.OrderItems.Count)
        {
            order.OrderItems.RemoveAt(index);
        }
    }
}
